﻿using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class BreedingInfoUpdateHandler : IRequestHandler<BreedingInfoUpdateRequest, ApiResult<BreedingInfo>>
{
    private readonly IBaseRepository<BreedingInfo> _repository;
    public BreedingInfoUpdateHandler(IBaseRepository<BreedingInfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<BreedingInfo>> Handle(BreedingInfoUpdateRequest request, CancellationToken cancellationToken)
    {
        var entity = await _repository.GetByIdAsync(request.Id);
        if (entity == null)
            return ApiResult<BreedingInfo>.Fail("未找到要修改的数据");

        entity.Breed = request.Breed;
        entity.Source = request.Source;
        entity.FeedingDays = request.FeedingDays;
        entity.FeedingBrand = request.FeedingBrand;
        entity.FeedingManager = request.FeedingManager;
        entity.OutboundDate = request.OutboundDate;
        entity.BatchNo = request.BatchNo;

        try
        {
            await _repository.UpdateAsync(entity);
            return ApiResult<BreedingInfo>.Success(entity, "修改成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<BreedingInfo>.Fail($"修改失败: {ex.Message}");
        }
    }
}